/*
  Copyright 2009 Colin Prinn

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

package net.recursv.motific.pool;

import javassist.CannotCompileException;
import javassist.CtClass;
import javassist.NotFoundException;
import org.apache.commons.io.IOUtils;

import java.io.*;
import java.net.URL;

/**
 * Created by: recursv
 * Date: 31/07/2009
 * Time: 5:26:10 PM
 */
public class DirectoryOutputPool implements OutputPool {

    protected File _directory;

    @SuppressWarnings({"ResultOfMethodCallIgnored"})
    public DirectoryOutputPool(File directory) throws NullPointerException {
        _directory = directory;
        _directory.mkdirs();
    }

    @SuppressWarnings({"ResultOfMethodCallIgnored"})
    public void addClass(CtClass ctClass) throws NotFoundException, IOException, CannotCompileException {
        File classFile = getOutputFile(ctClass);
        classFile.getParentFile().mkdirs();
        classFile.delete();
        write(new ByteArrayInputStream(ctClass.toBytecode()), classFile);
    }

    protected File getOutputFile(CtClass ctClass) {
        return new File(_directory.getPath() + File.separatorChar + ctClass.getName().replace('.', File.separatorChar) + ".class");
    }

    /**
     * Write the stream to the class file
     *
     * @param in   the byte stream to write
     * @param file the output file
     * @throws java.io.IOException if an error occurs
     */
    private void write(InputStream in, File file) throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        IOUtils.copy(in, baos);
        baos.flush();

        ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
        FileOutputStream out = new FileOutputStream(file);
        IOUtils.copy(bais, out);
        out.flush();
        out.close();
        in.close();


/*
        ByteArrayInputStream bais = new ByteArrayInputStream(testSuite.toBytecode());
        FileOutputStream out = new FileOutputStream(classFile);
        IOUtils.copy(bais, out);
        IOUtils.closeQuietly(bais);
        IOUtils.closeQuietly(out);
*/
    }

    @SuppressWarnings({"ResultOfMethodCallIgnored"})
    public void addFile(URL url, String filename) throws IOException {
        File temp;
        int lastIndex = filename.lastIndexOf(File.separatorChar);
        if (lastIndex == -1) {
            temp = _directory;
        } else {
            temp = new File(_directory, filename.substring(0, lastIndex));
        }
        if (!temp.exists()) {
            temp.mkdirs();
        }
        write(url.openStream(), new File(temp, filename.substring(lastIndex + 1)));
    }

}